package com.kingint.web.admin.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.mbg.ser.entity.SerCycleCleanDetails;
import com.kingint.mbg.ser.entity.SerCycleCleanPlan;
import com.kingint.mbg.ser.mapper.SerCycleCleanPlanMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.serve.service.SerCycleCleanDetailsService;
import com.kingint.web.admin.serve.service.SerCycleCleanPlanService;
import com.kingint.web.portal.serve.service.CycleCleanDetailsService;
import com.kingint.web.portal.serve.service.CycleCleanPlanService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.sys.service.UmsMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 周期卫生计划表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2021-01-11
 */
@Service
public class SerCycleCleanPlanServiceImpl extends ServiceImpl<SerCycleCleanPlanMapper, SerCycleCleanPlan> implements SerCycleCleanPlanService {

    @Resource
    private SerCycleCleanDetailsService detailsService;
    @Resource
    private UmsAdminService adminService;

    /**
     * 创建周期计划
     * @param serCycleCleanPlan
     * @return
     */
    @Override
    public boolean createPlan(SerCycleCleanPlan serCycleCleanPlan) {
        UmsMember currentMember = adminService.getCurrentMember();//当前用户
        serCycleCleanPlan.setCreateTime(new Date());
        serCycleCleanPlan.setHotelId(currentMember.getHotelId());
        boolean save = this.save(serCycleCleanPlan);
        if (!save){
            return false;
        }
        serCycleCleanPlan.getSerCycleCleanDetails().setCleanPanId(serCycleCleanPlan.getId());
        serCycleCleanPlan.getSerCycleCleanDetails().setCreateTime(serCycleCleanPlan.getStartTime());
        serCycleCleanPlan.getSerCycleCleanDetails().setEndTime(serCycleCleanPlan.getEndTime());
        boolean b = detailsService.save(serCycleCleanPlan.getSerCycleCleanDetails());
        if (!b){
            return false;
        }
        return true;
    }

    /**
     * 更新周期任务
     * @param serCycleCleanPlan
     * @return
     */
    @Override
    public Boolean updatePlan(SerCycleCleanPlan serCycleCleanPlan) {
        boolean b = this.updateById(serCycleCleanPlan);
        if (!b){
            return false;
        }
        boolean b1 = detailsService.updateById(serCycleCleanPlan.getSerCycleCleanDetails());
        if (!b1){
            return false;
        }
        return true;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Override
    public SerCycleCleanPlan getonebyid(Integer id) {
        QueryWrapper<SerCycleCleanDetails> detailsQueryWrapper = new QueryWrapper<>();
        detailsQueryWrapper.eq("clean_pan_id",id);
        SerCycleCleanDetails one = detailsService.getOne(detailsQueryWrapper);
        SerCycleCleanPlan byId = this.getById(id);
        return byId.setSerCycleCleanDetails(one);
    }

    /**
     * 分页获取
     * @param page
     * @return
     */
    @Override
    public IPage<SerCycleCleanPlan> getpage(Page<SerCycleCleanPlan> page) {
        UmsMember currentMember = adminService.getCurrentMember();//当前用户
        IPage<SerCycleCleanPlan> allListpage = baseMapper.getAllListpage(page,currentMember.getHotelId());
        return allListpage;
    }

    @Override
    public List<SerCycleCleanPlan> getalllist() {
//        UmsMember currentMember = adminService.getCurrentMember();//当前用户
        baseMapper.getAllList();
        return null;
    }

    /**
     * 删除周期计划
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        SerCycleCleanPlan byId = this.getonebyid(id);
        this.removeById(id);
        detailsService.removeById(byId.getSerCycleCleanDetails().getId());
        return true;
    }
}
